#region License
// Copyright 2008-2009 Jeremy Skinner (http://www.jeremyskinner.co.uk)
// 
// Licensed under the Apache License, Version 2.0 (the "License"); 
// you may not use this file except in compliance with the License. 
// You may obtain a copy of the License at 
// 
// http://www.apache.org/licenses/LICENSE-2.0 
// 
// Unless required by applicable law or agreed to in writing, software 
// distributed under the License is distributed on an "AS IS" BASIS, 
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
// See the License for the specific language governing permissions and 
// limitations under the License.
// 
// The latest version of this file can be found at http://www.codeplex.com/FluentValidation
#endregion

using FluentValidation.Internal;

namespace FluentValidation.Tests
{
    using System.Collections.Generic;
    using NUnit.Framework;

    [TestFixture]
    public class CollectionValidatorTests
    {
        PersonValidator validator;
        Person person;

        [SetUp]
        public void Setup()
        {
            validator = new PersonValidator();
            person = new Person()
            {
                Orders = new List<Order>() {
					new Order { Amount = 5},
					new Order { ProductName = "Foo"}
				}
            };
        }

        [Test]
        public void Validates_collection()
        {
            var results = validator.Validate(person);
            TestHelpers.OutputErrorsToConsole(results);
            results.Errors.Count.ShouldEqual(3);

            results.Errors[1].PropertyName.ShouldEqual("Orders[0].ProductName");
            results.Errors[2].PropertyName.ShouldEqual("Orders[1].Amount");
        }

        [Test]
        public void Collection_should_be_explicitly_included_with_expression()
        {
            var exp = new NamedExpression<Person> { Expression = x => x.Orders, Identifier = "Orders" };
            var results = validator.Validate(person, exp);
            TestHelpers.OutputErrorsToConsole(results); 
            results.Errors.Count.ShouldEqual(2);
            
        }

        [Test]
        public void Collection_should_be_explicitly_included_with_string()
        {
            var results = validator.Validate(person, "Orders");
            TestHelpers.OutputErrorsToConsole(results);
            results.Errors.Count.ShouldEqual(2);
        }

        [Test]
        public void Collection_should_be_excluded()
        {
            var exp = new NamedExpression<Person> { Expression = x => x.Forename, Identifier = "Forename" };
            var results = validator.Validate(person, exp);
            TestHelpers.OutputErrorsToConsole(results);
            results.Errors.Count.ShouldEqual(0);
         }

        public class OrderValidator : AbstractValidator<Order>
        {
            public OrderValidator()
            {
                RuleFor("ProductName", x => x.ProductName).NotEmpty();
                RuleFor("Amount", x => x.Amount).NotEqual(0);
            }
        }

        public class PersonValidator : AbstractValidator<Person>
        {
            public PersonValidator()
            {
                RuleFor("Surname", x => x.Surname).NotNull();
                RuleFor("Orders", x => x.Orders).SetValidator(new OrderValidator());
            }
        }
    }
}